package org.port.contacts;

import java.util.List;


import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ContactListFragment extends ListFragment implements
		LoaderCallbacks<List<ContactEntry>> {
	ContactListAdapter mAdapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Log.d("FLOW", "ListFragment onActivityCreated()");
		setEmptyText("No Contact");
		mAdapter = new ContactListAdapter(getActivity());
		setListAdapter(mAdapter);
		setListShown(false);
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Log.i("LoaderCustom", "Item clicked: " + id);
		// super.onListItemClick(l, v, position, id);
	}

	@Override
	public Loader<List<ContactEntry>> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		Log.d("FLOW", "ListFragment onCreateLoader()");
		return new ContactsListLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<ContactEntry>> loader,
			List<ContactEntry> data) {
		Log.d("FLOW", "ListFragment onLoadFinished()");
		mAdapter.setData(data);
		if (isResumed()) {
			Log.d("FLOW", "ListFragment onLoadFinished() Resumed");
			setListShown(true);
		} else {
			Log.d("FLOW", "ListFragment onLoadFinished() No Resumed");
			setListShownNoAnimation(true);
		}
		// TODO Auto-generated method stub
	}

	@Override
	public void onLoaderReset(Loader<List<ContactEntry>> loader) {
		// TODO Auto-generated method stub
		Log.d("FLOW", "ListFragment onLoaderReset()");
		mAdapter.setData(null);
	}
}